package com.baseframe.blog.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baseframe.blog.entity.request.SnsBlogRequest;
import com.baseframe.blog.entity.vo.SnsBlogVO;
import com.baseframe.blog.service.SnsBlogService;
import com.baseframe.blog.sign.BlogStatus;
import com.baseframe.tool.container.Kv;
import com.baseframe.tool.result.R;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 博客管理控制器
 */
@RestController
@RequestMapping("/back/blog")
@AllArgsConstructor
public class BackSnsBlogController {

    private SnsBlogService snsBlogService;

    /**
     * 查看所有博客
     * @param page
     * @param request
     * @return
     */
    @GetMapping("/page")
    public R<Page<SnsBlogVO>> blogPageList(Page<SnsBlogVO> page, SnsBlogRequest request){


        return R.data(snsBlogService.blogPageList(page,request));
    }

    /**
     * 管理员审核博客
     * @param request
     * @return
     */
    @PostMapping("/examine")
    public R<Object> examine(@RequestBody SnsBlogRequest request){
        if (request.getId() == null || request.getStatus() == null){
            return R.fail("缺少必要请求参数");
        }

        BlogStatus status = BlogStatus.getStatusByCode(request.getStatus());
        if (status==null){
            R.fail("请求参数不合法");
        }


        return snsBlogService.examineBlog(request)?R.success():R.fail();
    }

    /**
     * 获取博客状态信息
     * @return
     */
    @GetMapping("/status")
    public R<List<Kv>> getBlogStatus(){

        List<Kv> blogStatusList = new ArrayList<>();
        BlogStatus[] values = BlogStatus.values();
        for (BlogStatus value : values) {
            if (value==BlogStatus.AUDIT){
                continue;
            }
            Kv kv = Kv.create();
            if (value==BlogStatus.PUBLISHED){
                kv.put("key",BlogStatus.PUBLISHED.getCode());
                kv.put("value","通过");

            }else {
                kv.put("key",BlogStatus.REVIEW_FAILED.getCode());
                kv.put("value","驳回");
            }
            blogStatusList.add(kv);

        }

        return R.data(blogStatusList);
    }





}
